package org.yaglyf.rest.service;

import com.alibaba.dubbo.common.json.JSON;
import org.springframework.stereotype.Service;
import org.yaglyf.api.entity.UserInfo;
import org.yaglyf.api.service.UserInfoService;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author qiang.yang
 * @version 1.0.0
 * @ClassName: UserInfoServiceImpl
 * @Description:
 * @email qiang.yang03@ppdai.com
 * @date: 2018/11/12 17:52
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    private String address = "127.0.0.1";
    private int PORT = 9090;

    @Override
    public UserInfo testINFO() {
        try (Socket socket = new Socket(address, PORT)){
            OutputStream outputStream = socket.getOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject("testSocket");

            InputStream inputStream = socket.getInputStream();
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            Object o = objectInputStream.readObject();
            String value = String.valueOf(o);
            System.out.println(value);
            return JSON.parse(value,UserInfo.class);
        }catch (Exception e){

        }
        return null;
    }
}
